<html>
<head><meta charset="utf-8"><title>Problems with issue clippy#6467 · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html">Problems with issue clippy#6467</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225292840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225292840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225292840">(Feb 05 2021 at 12:35)</a>:</h4>
<p>Hi there!<br>
I don't know if this is the right place to ask, but I've ran into an issue while working <a href="https://github.com/rust-lang/rust-clippy/issues/6467">clippy#6467</a>. The goal is to check, if the last expression of a <code>Block</code> that returns <code>()</code> is terminated with a <code>;</code>. </p>
<p>I first tested the lint on the <code>println!()</code> macro where it worked alright. Looking at <code>assert_eq!()</code>, my lint fired even with a <code>;</code> at the end of the line. As I assumed the <code>assert_eq!()</code> macro implementation doesn't end with a semicolon, but the <code>println!()</code> macro does. I know this might be a beginner question, but is there a way to check for a semicolon after the macro invocation? I've already experimented with in_macro but it didn't quite help.</p>



<a name="225296846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225296846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225296846">(Feb 05 2021 at 13:17)</a>:</h4>
<p>Hey <span class="user-mention" data-user-id="358242">@Bastian</span>  this is the right place to ask. Could you maybe provide some example code where it fires on <code>assert_eq!()</code>?</p>
<p>Another small note: Hashtags usually link to the rust reop, you can use <code>clippy#</code> to like it to the clippy issue like this: <a href="https://github.com/rust-lang/rust-clippy/issues/6467">clippy#6467</a> <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="225297654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225297654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225297654">(Feb 05 2021 at 13:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="369415">xFrednet</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225296846">said</a>:</p>
<blockquote>
<p>Hey <span class="user-mention silent" data-user-id="358242">Bastian</span>  this is the right place to ask. Could you maybe provide some example code where it fires on <code>assert_eq!()</code>?</p>
<p>Another small note: Hashtags usually link to the rust reop, you can use <code>clippy#</code> to like it to the clippy issue like this: <a href="https://github.com/rust-lang/rust-clippy/issues/6467">clippy#6467</a> <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>
</blockquote>
<p>Ah alright great! Thank you. I've edited my post to refer to the clippy repo. </p>
<p>Here is some example code that shows how lint works with e.g. <code>println!()</code>or custom methods:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"Hello"</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;--</span><span class="w"> </span><span class="n">Works</span><span class="p">,</span><span class="w"> </span><span class="n">so</span><span class="w"> </span><span class="n">it</span><span class="w"> </span><span class="n">lints</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">line</span><span class="w"> </span><span class="n">saying</span><span class="w"> </span><span class="n">there</span><span class="w"> </span><span class="n">is</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">missing</span><span class="w"> </span><span class="sc">';'</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>but it also lints situations like this, where the lint should not be triggered:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">print_sum</span><span class="p">(</span><span class="n">a</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">assert_eq!</span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span><span class="w"> </span><span class="o">&lt;---</span><span class="w"> </span><span class="n">lints</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">line</span><span class="p">,</span><span class="w"> </span><span class="n">but</span><span class="w"> </span><span class="n">shouldn</span><span class="o">'</span><span class="na">t</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="225297899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225297899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225297899">(Feb 05 2021 at 13:28)</a>:</h4>
<p>Okay, I have an idea, could you check if it lints on this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">panic!</span><span class="p">(</span><span class="s">"Where did I go wrong?"</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="225298157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225298157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225298157">(Feb 05 2021 at 13:30)</a>:</h4>
<p>Have you pushed your code to a branch that we can look at? <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="225298393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225298393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225298393">(Feb 05 2021 at 13:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="369415">xFrednet</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225298157">said</a>:</p>
<blockquote>
<p>Have you pushed your code to a branch that we can look at? <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>
</blockquote>
<p>Sure! It's upon my fork <a href="https://github.com/1c3t3a/rust-clippy/blob/1c3t3a-issue-6467/clippy_lints/src/semicolon_if_nothing_returned.rs">here</a> :)</p>



<a name="225298467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225298467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225298467">(Feb 05 2021 at 13:33)</a>:</h4>
<p><span class="user-mention silent" data-user-id="369415">xFrednet</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225297899">said</a>:</p>
<blockquote>
<p>Okay, I have an idea, could you check if it lints on this:</p>
<p><div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">panic!</span><span class="p">(</span><span class="s">"Where did I go wrong?"</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div><br>
</p>
</blockquote>
<p>Yeah I'm trying something like this as well! I'll report in a minute ;)</p>



<a name="225298777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225298777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225298777">(Feb 05 2021 at 13:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="358242">Bastian</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225298467">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="369415">xFrednet</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225297899">said</a>:</p>
<blockquote>
<p>Okay, I have an idea, could you check if it lints on this:</p>
<p><div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">panic!</span><span class="p">(</span><span class="s">"Where did I go wrong?"</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div><br>
</p>
</blockquote>
<p>Yeah I'm trying something like this as well! I'll report in a minute ;)</p>
</blockquote>
<p>Yip, this lints the call to <code>panic!("Where did I go wrong?")</code>,  adding the <code>;</code> helps. So I assume this does originate within the assert_eq! macro, doesn't it?</p>



<a name="225298909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225298909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225298909">(Feb 05 2021 at 13:38)</a>:</h4>
<p>Okay, here is my theory:<br>
<a href="https://doc.rust-lang.org/src/core/macros/mod.rs.html#47">assert_eq!()</a> is expanded to a block which ends with a <code>panic!()</code> that has no <code>;</code> at the end. It might be that the expansion actually triggers  the lint. Most other lints start with an if block like this:<br>
<code>if !expr.span.from_expansion() {</code></p>
<p>This is basically what you tried with <code>in_macro</code> I guess</p>



<a name="225298942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225298942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225298942">(Feb 05 2021 at 13:38)</a>:</h4>
<p><code>in_macro</code> should do the same as <code>expr.span.from_expansion()</code> though</p>



<a name="225298976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225298976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225298976">(Feb 05 2021 at 13:39)</a>:</h4>
<p>could you try to add this as a first condition to your <code>if_chain!</code> ?<br>
Meaning the negation i.e. _if not in macro_</p>



<a name="225299221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225299221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225299221">(Feb 05 2021 at 13:41)</a>:</h4>
<p>Rustc usually adds a note if a lint is triggered within macro generated code. can you find something like this in the clippy output?</p>



<a name="225299229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225299229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225299229">(Feb 05 2021 at 13:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="369415">xFrednet</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225298909">said</a>:</p>
<blockquote>
<p>Okay, here is my theory:<br>
<a href="https://doc.rust-lang.org/src/core/macros/mod.rs.html#47">assert_eq!()</a> is expanded to a block which ends with a <code>panic!()</code> that has no <code>;</code> at the end. It might be that the expansion actually triggers  the lint. Most other lints start with an if block like this:<br>
<code>if !expr.span.from_expansion() {</code></p>
<p>This is basically what you tried with <code>in_macro</code> I guess</p>
</blockquote>
<p>Alright I see, that's what I thought as well. But this means we are ignoring all cases where the error comes from a macro. I that what we want? Because the code like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">assert_eq!</span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>would not be linted at all.</p>



<a name="225299334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225299334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225299334">(Feb 05 2021 at 13:42)</a>:</h4>
<p><span class="user-mention silent" data-user-id="369415">xFrednet</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225299221">said</a>:</p>
<blockquote>
<p>Rustc usually adds a note if a lint is triggered within macro generated code. can you find something like this in the clippy output?</p>
</blockquote>
<p>Ah yes, he is telling me the following: <code>note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)</code></p>



<a name="225299555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225299555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225299555">(Feb 05 2021 at 13:45)</a>:</h4>
<p>You should add a <code>in_macro(block.span)</code> to the lint. Then </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">assert_eq!</span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>should still get linted.</p>
<p>FYI: You can check a macro expansion on the playground under tools-&gt;expand macros</p>



<a name="225299707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225299707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225299707">(Feb 05 2021 at 13:46)</a>:</h4>
<p>Okay, we would obviously want to lint in these cases as well. The solution to this problem is sadly a bit outside of my current knowledge <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span>.  The last idea I have is to use <code>span.source_callsite()</code> this basically returns you the actual span of the source code. Maybe you can find some solution using this? (Assuming that <code>span.source_callsite()</code> is still within the macro for the generated panic)</p>



<a name="225300066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225300066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225300066">(Feb 05 2021 at 13:50)</a>:</h4>
<p>A hint for further development of this lint: You don't want to lint if a "block-like" expression is the last expression in a block (i.e. if/match/loop expressions)</p>



<a name="225300153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225300153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225300153">(Feb 05 2021 at 13:50)</a>:</h4>
<p><span class="user-mention silent" data-user-id="264664">flip1995</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225299555">said</a>:</p>
<blockquote>
<p>FYI: You can check a macro expansion on the playground under tools-&gt;expand macros</p>
</blockquote>
<p>That is such a nice tool! I always used a hacky cargo extention</p>



<a name="225300219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225300219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225300219">(Feb 05 2021 at 13:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="264664">flip1995</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225299555">said</a>:</p>
<blockquote>
<p>You should add a <code>in_macro(block.span)</code> to the lint. Then </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">assert_eq!</span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>should still get linted.</p>
<p>FYI: You can check a macro expansion on the playground under tools-&gt;expand macros</p>
</blockquote>
<p>Ah this works! But why? :D</p>



<a name="225300326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225300326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225300326">(Feb 05 2021 at 13:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="264664">flip1995</span>  is a magician ^^</p>



<a name="225300438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225300438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225300438">(Feb 05 2021 at 13:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="264664">flip1995</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225300066">said</a>:</p>
<blockquote>
<p>A hint for further development of this lint: You don't want to lint if a "block-like" expression is the last expression in a block (i.e. if/match/loop expressions)</p>
</blockquote>
<p>I see, so I should also consider checking <code>ExprKind</code>of the last <code>Expr</code> in a block?</p>



<a name="225300905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225300905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225300905">(Feb 05 2021 at 13:58)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="kc">false</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">left_val</span><span class="p">,</span><span class="w"> </span><span class="n">right_val</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="p">(</span><span class="o">*</span><span class="n">left_val</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="o">*</span><span class="n">right_val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span>::<span class="n">core</span>::<span class="n">panicking</span>::<span class="n">panic_fmt</span><span class="p">(</span>::<span class="n">core</span>::<span class="n">fmt</span>::<span class="n">Arguments</span>::<span class="n">new_v1</span><span class="p">(</span><span class="o">&amp;</span><span class="p">[</span><span class="s">"assertion failed: `(left == right)`</span><span class="se">\n</span><span class="s">  left: `"</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                  </span><span class="s">"`,</span><span class="se">\n</span><span class="s"> right: `"</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                  </span><span class="s">"`"</span><span class="p">],</span><span class="w"></span>
<span class="w">                                                                                </span><span class="o">&amp;</span><span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;&amp;*</span><span class="n">left_val</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                        </span><span class="o">&amp;&amp;*</span><span class="n">right_val</span><span class="p">)</span><span class="w"></span>
<span class="w">                                                                                     </span><span class="p">{</span><span class="w"></span>
<span class="w">                                                                                     </span><span class="p">(</span><span class="n">arg0</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                      </span><span class="n">arg1</span><span class="p">)</span><span class="w"></span>
<span class="w">                                                                                     </span><span class="o">=&gt;</span><span class="w"></span>
<span class="w">                                                                                     </span><span class="p">[</span>::<span class="n">core</span>::<span class="n">fmt</span>::<span class="n">ArgumentV1</span>::<span class="n">new</span><span class="p">(</span><span class="n">arg0</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                                                   </span>::<span class="n">core</span>::<span class="n">fmt</span>::<span class="n">Debug</span>::<span class="n">fmt</span><span class="p">),</span><span class="w"></span>
<span class="w">                                                                                      </span>::<span class="n">core</span>::<span class="n">fmt</span>::<span class="n">ArgumentV1</span>::<span class="n">new</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                                                   </span>::<span class="n">core</span>::<span class="n">fmt</span>::<span class="n">Debug</span>::<span class="n">fmt</span><span class="p">)],</span><span class="w"></span>
<span class="w">                                                                                 </span><span class="p">}))</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This is the expansion of the <code>assert_eq</code> macro. <code>panic_fmt</code> returns <code>!</code>, but since <code>!</code> is inferred to every type, the outer most block returns <code>()</code>, because the function returns <code>()</code>. So the lint, as you implemented it triggers on the <code>match</code> block inside the block of the macro expansion, because type inference determines that it has to be <code>()</code>. So that is the explanation why the expansion of the assert gets linted.</p>
<p><code>in_macro(block.span)</code> works, because it will recognize that the outer most block of the expansion comes from the macro and won't lint. But for the block coming from the function body it will find for the <code>block.expr</code> the outermost block of the assert expansion and notices, that it doesn't have a semicolon.</p>



<a name="225301032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225301032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225301032">(Feb 05 2021 at 13:59)</a>:</h4>
<p>That is super intelligent and simple at the same time! <span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span></p>



<a name="225301075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225301075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225301075">(Feb 05 2021 at 13:59)</a>:</h4>
<p><span class="user-mention silent" data-user-id="358242">Bastian</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225300438">said</a>:</p>
<blockquote>
<p>I see, so I should also consider checking <code>ExprKind</code>of the last <code>Expr</code> in a block?</p>
</blockquote>
<p>Yes, but only if the <code>block.expr</code> <strong>doesn't</strong> come from a macro. If it comes from a macro expansion, you can lint independent of the kind of <code>block.expr</code></p>



<a name="225301402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225301402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225301402">(Feb 05 2021 at 14:02)</a>:</h4>
<p><span class="user-mention silent" data-user-id="369415">xFrednet</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225300153">said</a>:</p>
<blockquote>
<p>That is such a nice tool! I always used a hacky cargo extention</p>
</blockquote>
<p><a href="https://github.com/dtolnay/cargo-expand"><code>cargo expand</code></a> is also a really useful command if you need to get the expansion for a whole crate instead of a single file. But if you need this to fix something in Clippy, you know that it'll be pain.</p>



<a name="225302281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225302281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225302281">(Feb 05 2021 at 14:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="264664">flip1995</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225300905">said</a>:</p>
<blockquote>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="kc">false</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">left_val</span><span class="p">,</span><span class="w"> </span><span class="n">right_val</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="p">(</span><span class="o">*</span><span class="n">left_val</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="o">*</span><span class="n">right_val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span>::<span class="n">core</span>::<span class="n">panicking</span>::<span class="n">panic_fmt</span><span class="p">(</span>::<span class="n">core</span>::<span class="n">fmt</span>::<span class="n">Arguments</span>::<span class="n">new_v1</span><span class="p">(</span><span class="o">&amp;</span><span class="p">[</span><span class="s">"assertion failed: `(left == right)`</span><span class="se">\n</span><span class="s">  left: `"</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                  </span><span class="s">"`,</span><span class="se">\n</span><span class="s"> right: `"</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                  </span><span class="s">"`"</span><span class="p">],</span><span class="w"></span>
<span class="w">                                                                                </span><span class="o">&amp;</span><span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;&amp;*</span><span class="n">left_val</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                        </span><span class="o">&amp;&amp;*</span><span class="n">right_val</span><span class="p">)</span><span class="w"></span>
<span class="w">                                                                                     </span><span class="p">{</span><span class="w"></span>
<span class="w">                                                                                     </span><span class="p">(</span><span class="n">arg0</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                      </span><span class="n">arg1</span><span class="p">)</span><span class="w"></span>
<span class="w">                                                                                     </span><span class="o">=&gt;</span><span class="w"></span>
<span class="w">                                                                                     </span><span class="p">[</span>::<span class="n">core</span>::<span class="n">fmt</span>::<span class="n">ArgumentV1</span>::<span class="n">new</span><span class="p">(</span><span class="n">arg0</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                                                   </span>::<span class="n">core</span>::<span class="n">fmt</span>::<span class="n">Debug</span>::<span class="n">fmt</span><span class="p">),</span><span class="w"></span>
<span class="w">                                                                                      </span>::<span class="n">core</span>::<span class="n">fmt</span>::<span class="n">ArgumentV1</span>::<span class="n">new</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span><span class="w"></span>
<span class="w">                                                                                                                   </span>::<span class="n">core</span>::<span class="n">fmt</span>::<span class="n">Debug</span>::<span class="n">fmt</span><span class="p">)],</span><span class="w"></span>
<span class="w">                                                                                 </span><span class="p">}))</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This is the expansion of the <code>assert_eq</code> macro. <code>panic_fmt</code> returns <code>!</code>, but since <code>!</code> is inferred to every type, the outer most block returns <code>()</code>, because the function returns <code>()</code>. So the lint, as you implemented it triggers on the <code>match</code> block inside the block of the macro expansion, because type inference determines that it has to be <code>()</code>. So that is the explanation why the expansion of the assert gets linted.</p>
<p><code>in_macro(block.span)</code> works, because it will recognize that the outer most block of the expansion comes from the macro and won't lint. But for the block coming from the function body it will find for the <code>block.expr</code> the outermost block of the assert expansion and notices, that it doesn't have a semicolon.</p>
</blockquote>
<p>Haha this is great! Thanks a lot! I've actually took a look at the macro expansion of the playground and it's actually a great tool for understanding how macros work.  Thanks again ;)</p>



<a name="225302861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225302861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225302861">(Feb 05 2021 at 14:14)</a>:</h4>
<p><span class="user-mention silent" data-user-id="264664">flip1995</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225301075">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="358242">Bastian</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225300438">said</a>:</p>
<blockquote>
<p>I see, so I should also consider checking <code>ExprKind</code>of the last <code>Expr</code> in a block?</p>
</blockquote>
<p>Yes, but only if the <code>block.expr</code> <strong>doesn't</strong> come from a macro. If it comes from a macro expansion, you can lint independent of the kind of <code>block.expr</code></p>
</blockquote>
<p>Ok, so this would mean keeping the if  <code>!in_macro(block.span);</code> statement as the first condition of my <code>if_chain!</code>.</p>



<a name="225303191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225303191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225303191">(Feb 05 2021 at 14:16)</a>:</h4>
<p>yes and adding a <code>if in_macro(block.expr.span) { lint } else { lint only for some ExprKinds }</code> in the <code>then</code> of the <code>if_chain!</code>. Which is obviously pseudo code. I have not tested anything about this, so take it with a grain of salt.</p>



<a name="225305685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225305685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225305685">(Feb 05 2021 at 14:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="264664">flip1995</span> <a href="#narrow/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467/near/225303191">said</a>:</p>
<blockquote>
<p>yes and adding a <code>if in_macro(block.expr.span) { lint } else { lint only for some ExprKinds }</code> in the <code>then</code> of the <code>if_chain!</code>. Which is obviously pseudo code. I have not tested anything about this, so take it with a grain of salt.</p>
</blockquote>
<p>Alright, thanks a lot! Also to <span class="user-mention" data-user-id="369415">@xFrednet</span> for the quick help! I'll resume implementing this ;)</p>



<a name="225332463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Problems%20with%20issue%20clippy%236467/near/225332463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bastian <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Problems.20with.20issue.20clippy.236467.html#225332463">(Feb 05 2021 at 17:51)</a>:</h4>
<p>Oke so I implemented the lint but it fails the <code>dogfood</code> lints.... mostly because macros like <code>if_chain! {}</code> are not terminated by a <code>;</code>... how should I handle this? Or is it better to already draft a PR where we can discuss about it?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>